// pages/demand/demand-list.js
const privince_short = require('../../../utils/city_short.js')
const app = getApp()
Page({

    data: {
        showFilter: false,
        privince_short,
        grade: [{
                name: '1~5分',
                leveal: 1
            },
            {
                name: '5~12分',
                leveal: 2
            },
            {
                name: '12分以上',
                leveal: 3
            },
        ],
        createSort: 'createTime.desc',
        endSort: 'endTime.desc',
        leveal: null,
        belong: null,
        happen: null,
        page: 1,
        total: 2,
        loading: false,
        list: []
    },
    onLoad: function(options) {
        options.pcCode = wx.getStorageSync('pcCode')
        this.setData(options)
        wx.showLoading({
            title: '加载中...',
        })
        this.loadData()
    },
    isShowFilter() {
        this.setData({
            showFilter: !this.data.showFilter
        })
    },
    loadData() {
        let {
            page = 1, total = 2, loading = false, list = [], belong = null, happen = null, leveal = null, createSort = 'createTime.desc', endSort = 'endTime.desc', keywords = ''
        } = this.data
        if (list.length >= total || loading) return
        this.setData({
            loading: true
        })
        if (this.data.pcCode) {
            let current_city = this.data.privince_short.find(o => o.city_code == this.data.pcCode)
            belong = happen = current_city.city_short
        }
        let data = {
            page,
            rows: 10,
            orderString: createSort + ',' + endSort,
        }
        if (belong) data.carNo = belong
        if (happen) data.provinces = happen
        if (leveal) data.leveal = leveal
        if (keywords) data.carNo = keywords
        app.http.post('rest/ticket/queryByList.do', {
            data: data
        }).then(res => {
            page++
            total = res.obj.total
            loading = false
            list.push(...res.obj.rows)
            this.setData({
                page,
                total,
                loading,
                list,
                belong,
                happen
            })
            if (keywords) {
                wx.setNavigationBarTitle({
                    title: '关于' + keywords + '的需求列表',
                })
            }
            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(err => {
            this.setData({
                loading: false
            })
        })
    },

    //筛选
    filterGrade(evt) {
        this.setData({
            leveal: evt.currentTarget.dataset.leveal
        })
    },
    filterBelong(evt) {
        this.setData({
            belong: evt.currentTarget.dataset.belong
        })
    },
    filterHappen(evt) {
        this.setData({
            happen: evt.currentTarget.dataset.happen
        })
    },
    //排序
    bindsort(evt) {
        let sort = evt.currentTarget.dataset.sort
        let {
            createSort = 'createTime.desc', endSort = 'endTime.desc',
        } = this.data
        if (sort == 'create') {
            if (createSort == 'createTime.desc') {
                createSort = 'createTime.asc'
            } else {
                createSort = 'createTime.desc'
            }
        }
        if (sort == 'end') {
            if (endSort == 'endTime.desc') {
                endSort = 'endTime.asc'
            } else {
                endSort = 'endTime.desc'
            }
        }
        this.setData({
            createSort,
            endSort
        })
        this.onPullDownRefresh()
    },
    //reset
    reset() {
        this.setData({
            leveal: null,
            belong: null,
            happen: null,
            pcCode: ''
        })
        this.onPullDownRefresh()
        this.isShowFilter()
    },
    submit() {
        this.onPullDownRefresh()
        this.isShowFilter()
    },
    toDetail(evt) {
        let {
            quoteid,
            id
        } = evt.currentTarget.dataset
        if (quoteid) { //已报价
            wx.showModal({
                title: '温馨提示',
                content: '您已参与过该违章单的报价,是否查看报价详情',
                confirmColor: '#2691df',
                success: res => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: './offer-success?id=' + id + '&cid=' + quoteid,
                        })
                    }
                }
            })
            return
        }
        if (app.getPrevPage() && app.getPrevPage().route == 'pages/demand/offer-success') {
            wx.redirectTo({
                url: './demand-detail?id=' + id,
            })
        } else {
            wx.navigateTo({
                url: './demand-detail?id=' + id,
            })
        }

    },
    onPullDownRefresh: function() {
        this.setData({
            page: 1,
            total: 2,
            loading: false,
            list: []
        })
        this.loadData()
    },
    onReachBottom: function() {
        this.loadData()
    },

})